//
//  CarClassEntity.m
//  webRequestText
//
//  Created by 肖扬 on 15/9/6.
//  Copyright (c) 2015年 肖扬. All rights reserved.
//


#import "CarClassEntity.h"

@implementation CarClassEntity

-(id) initWithFMDB:(FMDatabase *)database{
  self = [super initWithFMDB:database];
  if(self){
    //给表名赋值
    self.tableName = @"CarClass";
    //字段列表
    self.fields = @[@"ClassID", @"ClassName", @"isDel", @"CreateTime", @"LastUpdate", @"Version", @"userid"];
    //主键的ID
//    self.primaryField = @"id";
    [self setDefault];
  }
  return self;
}

//设置默认数据
-(void) setDefault{
  //清除数据
    self.ClassID = nil;
    self.isDel = nil;
    self.ClassName = nil;
    self.CreateTime = nil;
    self.LastUpdate = nil;
    self.Version = nil;
    self.userid = nil;
}
//获取字段存到数据库的值
-(NSArray *) upDates{
    return @[[XYSqlite nilFilter: self.ClassName],
             [XYSqlite nilFilter: self.CreateTime],
             [XYSqlite nilFilter: self.LastUpdate],
             [XYSqlite nilFilter: self.Version],
             [XYSqlite nilFilter: self.userid],
             [XYSqlite nilFilter: self.isDel],
             [XYSqlite nilFilter: self.ClassID]];
}

//获取所有字段存到数据库的值
-(NSArray *) parameters{
  return @[[XYSqlite nilFilter: self.ClassName],
           [XYSqlite nilFilter: self.CreateTime],
           [XYSqlite nilFilter: self.LastUpdate],
           [XYSqlite nilFilter: self.Version],
           [XYSqlite nilFilter: self.userid],
           [XYSqlite nilFilter: self.isDel],
           [XYSqlite nilFilter: self.ClassID]];
}

//插入数据
-(NSInteger)insert{
    NSString *sql = @"INSERT INTO CarClass(%@) VALUES (%@)";
    NSString *insertFields = @"ClassName, isDel, CreateTime, LastUpdate, Version, userid";
    NSString *insertValues = @"?,?,?,?,?,?";
    NSLog(@"ClassID___%@", self.ClassID);
    //没有指定ID
    if(self.ClassID != nil){
    insertFields = [insertFields stringByAppendingString: @", ClassID"];
    insertValues = [insertValues stringByAppendingString: @", ?"];
    }
    sql = [NSString stringWithFormat: sql, insertFields, insertValues];
    return [self insertWithSql:sql parameters: [self parameters]];
}

//更新数据
-(BOOL)update{
    NSString *sql = @"UPDATE CarClass SET ClassName = ?, CreateTime = ?, LastUpdate = ?, Version = ?, userid = ?, isDel = ? WHERE ClassName = ?";
    [self.database open];
    BOOL result = [self.database executeUpdate:sql withArgumentsInArray: [self parameters]];
    [self.database close];
    return result;
}
//更新版本
-(BOOL)updateVersion:(NSString *)version{
    NSString *sql = @"UPDATE CarClass SET Version = ? WHERE ClassName = ?";
    [self.database open];
    BOOL result = [self.database executeUpdate:sql withArgumentsInArray: [self parameters]];
    [self.database close];
    return result;
}
//删除数据
-(BOOL)deldate:(NSString *)isDel{
    NSString *sql = @"UPDATE CarClass SET isDel = ? WHERE ClassName = ?";
    [self.database open];
    BOOL result = [self.database executeUpdate:sql withArgumentsInArray: [self parameters]];
    [self.database close];
    return result;
}

//转换字典到当前实例
-(void)parseFromDictionary: (NSDictionary *) data{
    self.data       = data;
    self.ClassID    = [XYSqlite valueToString: [data objectForKey:@"isDel"]];
    self.isDel      = [XYSqlite valueToString: [data objectForKey:@"isDel"]];
    self.ClassName  = [XYSqlite valueToString: [data objectForKey:@"ClassName"]];
    self.CreateTime = [XYSqlite valueToString: [data objectForKey:@"CreateTime"]];
    self.LastUpdate = [XYSqlite valueToString: [data objectForKey:@"LastUpdate"]];
    self.Version    = [XYSqlite valueToString: [data objectForKey:@"Version"]];
    self.userid     = [XYSqlite valueToString: [data objectForKey:@"userid"]];
}

//获取一条数据
-(BOOL) findOneWithCondition:(NSString *)cond parameters:(NSArray *)params orderBy:(NSString *)orderBy{
    BOOL result = [super findOneWithCondition:cond parameters:params orderBy:orderBy];
    //将数据填充到属性
    if(result){
    [self parseFromDictionary: self.data];
    }else{
    //没有找到数据，还原为初始值
    [self setDefault];
    }
    return result;
}

//==================获取字段名及表名==================
//获取所有的字段名称
+(NSArray *) fields{
    return @[@"ClassID", @"ClassName", @"isDel", @"CreateTime", @"LastUpdate", @"Version", @"userid"];
}

//获取表名
+(NSString*)tableName{
    return @"CarClass";
}

//字段名：ClassID
+(NSString*)ClassID{
    return @"ClassID";
}

//字段名： ClassName
+(NSString*)ClassName{
    return @"ClassName";
}

//字段名：isDel
+(NSString*)isDel{
    return @"isDel";
}

//字段名：CreateTime
+(NSString*)CreateTime{
    return @"CreateTime";
}

//字段名：LastUpdate
+(NSString *) LastUpdate{
    return @"LastUpdate";
}

//字段名：Version
+(NSString *) Version{
    return @"Version";
}

//字段名：userid
+(NSString *) userid{
    return @"userid";
}

//创建数据库的sql语句
+(NSString *) sqlForCreateTable{
    return @"CREATE TABLE if not exists CarClass(ClassID TEXT, ClassName TEXT, isDel TEXT, CreateTime TEXT, LastUpdate TEXT, Version TEXT, userid TEXT)";
}
@end
